<?php
/**
 * @copyright        2016 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           TL <mengwb@opencart.cn>
 * @created          2016-12-13 17:04:00
 * @modified         2016-12-13 17:04:00
 */

class ControllerWeixinloginGetOpenid extends Controller
{
    public function index()
    {
        $route = '';
        if (!$this->config->get('weipay_status')) {
            return;
        }

        if (isset($this->request->get['route'])) {
            $part = explode('/', $this->request->get['route']);

            if (isset($part[0])) {
                $route .= $part[0];
            }
        }
        if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && $route != 'weixinlogin' && (!isset($this->request->get['route']) || ($this->request->get['route'] != 'account/logout'))) {
            if (!$this->customer->isLogged()) {
                if(isset($this->request->get['openid'])) {  //非认证账号通过get传递Openid
                    $this->session->data['userinfo'] = array('openid'=>$this->request->get['openid']);
                }
                if (!isset($this->session->data['wxredirect'])) {
                    $au = parse_url(HTTP_SERVER);
                    $url = $au['scheme'] . '://' . $au['host'] . $_SERVER["REQUEST_URI"];
                    $this->session->data['wxredirect'] = $url;
                }
                if (!isset($this->session->data['userinfo']) && !isset($this->request->get['openid'])) {
                    $this->weixinhome();
                }
            }
        }
    }

    public function weixinhome()
    {
        include_once("catalog/controller/extension/payment/WxPayPubHelper/WxPayPubHelper.php");
        //设置参数
        if($this->config->get('weipay_appid')){
            WxPayConf_pub::$APPID = $this->config->get('weipay_appid');
        }
        if($this->config->get('weipay_mchid')){
            WxPayConf_pub::$MCHID = $this->config->get('weipay_mchid');
        }
        if($this->config->get('weipay_key')){
            WxPayConf_pub::$KEY = $this->config->get('weipay_key');
        }
        if($this->config->get('weipay_appsecret')){
            WxPayConf_pub::$APPSECRET = $this->config->get('weipay_appsecret');
        }
        //使用jsapi接口
        $jsApi = new JsApi_pub();

        //=========步骤1：网页授权获取用户openid============
        //通过code获得openid
        if (!isset($_GET['code'])) {
            //触发微信返回code码
            $url = $jsApi->createOauthUrlForCode(HTTP_SERVER);
            Header("Location: $url");
            exit();
        } else {
            //获取code码，以获取openid
            $code = $_GET['code'];
            $jsApi->setCode($code);
            $openid = $jsApi->getOpenId();
            $userinfo = $jsApi->getUserinfo();
            //preg_replace('~\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]~', '', $userinfo['nickname'])改函数删除昵称中的表情，否则会导致乱码
            $userinfo['nickname'] = preg_replace('~\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]~', '', $userinfo['nickname']);
            $userinfo['access_token'] = '';
            $this->session->data['userinfo'] = $userinfo;
            if (!isset($this->session->data['userinfo']['unionid'])) {
              $this->session->data['userinfo']['unionid'] = '';
            }
        }
    }
}
